﻿Imports System.IO
Public Class frmSourceVarfile
    Private m_BaseSoruce As CAermod.BaseSource  '污染源基类
    Property BaseSource() As CAermod.BaseSource
        Get
            Return Me.m_BaseSoruce
        End Get
        Set(ByVal value As CAermod.BaseSource)
            Me.m_BaseSoruce = value
        End Set
    End Property
    Private m_IntialSelectedIndex As Integer = -1
    Property IntialSelectedIndex() As Integer
        Get
            Return Me.m_IntialSelectedIndex
        End Get
        Set(ByVal value As Integer)
            Me.m_IntialSelectedIndex = value
        End Set
    End Property
    Private Sub frmSourceVarfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.EFlex1.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlex1.Rows.Count = 1

   

        '设置表头
        If m_BaseSoruce.Location.Srcid.Substring(0, 1) = "P" Then
            Me.EFlex1.Cols.Count = m_BaseSoruce.PollutionPara.Length + 5
            Me.EFlex1.SetData(0, 0, " 序号")
            Me.EFlex1.SetData(0, 1, " 时间")
            Me.EFlex1.SetData(0, 2, " 烟气温度[K]")
            Me.EFlex1.SetData(0, 3, " 烟气速度[m/s]")
            For i As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                Dim s As String = m_BaseSoruce.PollutionPara(i).Name
                Me.EFlex1.SetData(0, 4 + i, s)
            Next
        Else
            Me.EFlex1.Cols.Count = m_BaseSoruce.PollutionPara.Length + 2
            Me.EFlex1.SetData(0, 0, " 序号")
            Me.EFlex1.SetData(0, 1, " 时间")
            For i As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                Dim s As String = m_BaseSoruce.PollutionPara(i).Name
                Me.EFlex1.SetData(0, 2 + i, s)
            Next
        End If


        Me.EFlex1.Rows.Count = 8785
        '初始化表格
        showtable()
        ' 设置单位
        showUnit()

        Me.EFlex1.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlex1.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlex1.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

    End Sub
    Private Sub showUnit()
        If m_BaseSoruce.Location.Srcid.Substring(0, 1) = "P" Then '点源
            Me.cmbPolluteUnit.Text = "g/s"
        ElseIf m_BaseSoruce.Location.Srcid.Substring(0, 1) = "A" Then '面源
            Me.cmbPolluteUnit.Text = "g/s•m^2"
        ElseIf m_BaseSoruce.Location.Srcid.Substring(0, 1) = "O" Then '敞口源
            Me.cmbPolluteUnit.Text = "g/s•m^2"
        ElseIf m_BaseSoruce.Location.Srcid.Substring(0, 1) = "V" Then '体源
            Me.cmbPolluteUnit.Text = "g/s"
        ElseIf m_BaseSoruce.Location.Srcid.Substring(0, 1) = "L" Then '线源L
            Me.cmbPolluteUnit.Text = "g/s•m"
        End If


    End Sub


    ''' <summary>
    ''' 初始化表格
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub showtable()
        Dim m_StartDate As Date = "2008年" + "01月" + "01日" + "00时"
        Dim m_ENDDate As Date = "2008年" + "12月" + "31日" + "00时"

        If m_BaseSoruce.IsSourceHour = True Then
            '循环8784小时
            For i As Integer = 0 To 8783

                '循环污染物
                If m_BaseSoruce.Location.Srcid.Substring(0, 1) = "P" Then
                    '循环污染物
                    For j As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                        '如果小时变化非空
                        If m_BaseSoruce.PollutionPara(j).HourEmisCollection.Count > 0 Then
                            Me.EFlex1.SetData(i + 1, 0, i + 1) '时间
                            Dim s As String = Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Month, "00") + "月" + Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Day, "00") + "日" + Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Hour, "00") + "时"
                            Me.EFlex1.SetData(i + 1, 1, s) '时间
                            Me.EFlex1.SetData(i + 1, 2, m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceTemp) '烟气温度(K)
                            Me.EFlex1.SetData(i + 1, 3, m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceExit) '烟气速度(m/s)
                            Me.EFlex1.SetData(i + 1, 4 + j, m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceEmis) '源强(g/s)
                        Else
                            Me.EFlex1.SetData(i + 1, 0, i + 1) '时间
                            Dim s As String = Format(m_StartDate.Month, "00") + "月" + Format(m_StartDate.Day, "00") + "日" + Format(m_StartDate.Hour, "00") + "时"
                            Me.EFlex1.SetData(i + 1, 1, s) '时间
                            Me.EFlex1.SetData(i + 1, 2, 0) '烟气温度(K)
                            Me.EFlex1.SetData(i + 1, 3, 0) '烟气速度(m/s)
                            Me.EFlex1.SetData(i + 1, 4 + j, 0) '源强(g/s)
                        End If
                    Next
                Else
                    For j As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                        If m_BaseSoruce.PollutionPara(j).HourEmisCollection.Count > 0 Then

                            Me.EFlex1.SetData(i + 1, 0, i + 1) '时间
                            Dim s As String = Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Month, "00") + "月" + Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Day, "00") + "日" + Format(m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate.Hour, "00") + "时"
                            Me.EFlex1.SetData(i + 1, 1, s) '时间
                            Me.EFlex1.SetData(i + 1, 2 + j, m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceEmis) '源强(g/s)
                        Else
                            Me.EFlex1.SetData(i + 1, 0, i + 1) '时间
                            Dim s As String = Format(m_StartDate.Month, "00") + "月" + Format(m_StartDate.Day, "00") + "日" + Format(m_StartDate.Hour, "00") + "时"
                            Me.EFlex1.SetData(i + 1, 1, s) '时间
                            Me.EFlex1.SetData(i + 1, 2 + j, 0) '源强(g/s)
                        End If

                    Next

                End If
                '时间+1
                m_StartDate = m_StartDate.AddHours(1)
            Next
        End If
        Me.EFlex1.AutoSizeCols()
    End Sub
    ''' <summary>
    ''' 确定事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok.Click
        '更新类
        upClass()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    Private Sub cannel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cannel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    ''' <summary>
    ''' 更新类
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub upClass()

        Dim flag As Integer = 0
        '源变化参数为真
        ' 清空集合
        For j As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
            m_BaseSoruce.PollutionPara(j).HourEmisCollection.Clear()
        Next

        For i As Integer = 0 To Me.EFlex1.Rows.Count - 2

            For j As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                m_BaseSoruce.PollutionPara(j).HourEmisCollection.Add(New CAermod.HourEmis)
                If m_BaseSoruce.Location.Srcid.Substring(0, 1) = "P" Then
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate = "2008年" + Me.EFlex1.GetData(i + 1, 1).ToString '时间
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceEmis = Me.EFlex1.GetData(i + 1, 4 + j) '污染物源强
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceTemp = Me.EFlex1.GetData(i + 1, 2) '温度
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceExit = Me.EFlex1.GetData(i + 1, 3) '烟气速度
                Else
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).Hdate = "2008年" + Me.EFlex1.GetData(i + 1, 1).ToString '时间
                    m_BaseSoruce.PollutionPara(j).HourEmisCollection.Item(i).SourceEmis = Me.EFlex1.GetData(i + 1, 2 + j) '污染物源强
                End If
            Next
        Next


    End Sub
    ''' <summary>
    ''' 更新表格
    ''' </summary>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <param name="emis"></param>
    ''' <param name="temp"></param>
    ''' <param name="esit"></param>
    ''' <remarks></remarks>
    Private Sub ModifyTable(ByVal StartDate As Date, ByVal EndDate As Date, ByVal emis() As Double, ByVal temp As Double, ByVal esit As Double)
        Dim m_StartDate As Date = "2008年" + "01月" + "01日" + "00时"
        Dim m_EndDate As Date = EndDate.ToString

        Dim Flagstart As Integer = 0

        While m_StartDate <= StartDate
            Flagstart = Flagstart + 1
            '日期加1小时，开始下一次循环
            m_StartDate = m_StartDate.AddHours(1)
        End While

        '循环时间 按小时,加入数据表格
        While StartDate <= EndDate

            '点源需要源强、温度和烟气速度三个参数 其余源1个参数
            If m_BaseSoruce.Location.Srcid.Substring(0, 1) = "P" Then

                '循环污染物
                Dim s As String = Format(StartDate.Month, "00") + "月" + Format(StartDate.Day, "00") + "日" + Format(StartDate.Hour, "00") + "时"

                Me.EFlex1.SetData(Flagstart, 0, Flagstart) '序号
                Me.EFlex1.SetData(Flagstart, 1, s) '时间
                Me.EFlex1.SetData(Flagstart, 2, temp) '温度
                Me.EFlex1.SetData(Flagstart, 3, esit) '烟气速度
                For i As Integer = 0 To emis.Length - 1 '污染物源强
                    Me.EFlex1.SetData(Flagstart, 4 + i, emis(i)) '污染物源强
                Next

            Else

                '循环污染物
                Dim s As String = Format(StartDate.Month, "00") + "月" + Format(StartDate.Day, "00") + "日" + Format(StartDate.Hour, "00") + "时"

                Me.EFlex1.SetData(Flagstart, 0, Flagstart) '序号
                Me.EFlex1.SetData(Flagstart, 1, s) '时间

                For i As Integer = 0 To emis.Length - 1 '污染物源强
                    Me.EFlex1.SetData(Flagstart, 2 + i, emis(i)) '污染物源强
                Next
            End If

            '日期加1小时，开始下一次循环
            StartDate = StartDate.AddHours(1)
            Flagstart = Flagstart + 1
        End While
        '自动调整列宽
        Me.EFlex1.AutoSizeCols()


    End Sub
    ''' <summary>
    ''' 设置排放时段
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub shezhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles shezhi.Click
        '设置时间范围
        Dim daterange As New frmDateRange
        daterange.BaseSource = m_BaseSoruce

        'daterange.StartTime = "01月01日00时"
        'daterange.EndTime = "12月31日23时"



            '取得各污染物设置的排放速率
            If daterange.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim kk(m_BaseSoruce.PollutionPara.Length - 1) As Double
                For i As Integer = 0 To m_BaseSoruce.PollutionPara.Length - 1
                kk(i) = daterange.SourcePolluent.GetData(i + 1, 1)
                Next
            '更新表格

            Dim StartDate As Date = "2008年" + daterange.StartTime

            Dim Enddate As Date = "2008年" + daterange.EndTime


            ModifyTable(StartDate, Enddate, kk, Val(daterange.temp.Text), Val(daterange.Esit.Text))
        End If
    End Sub
End Class
